﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace sjzyoukucaiji
{
    public class Err
    {
        private static string _logFileName="c:/err.txt";
        private static int _logFileSizes = 1024;


        /// <summary>
        /// 写入日志信息
        /// </summary>
        /// <param name="msg">日志内容</param>
        /// <param name="IsAutoDelete">是否自动删除日志</param>
        public static void writeInLog(string msg)
        {
            writeInLog(msg, _logFileSizes != 0);
        }

        /// <summary>
        /// 写入日志信息
        /// </summary>
        /// <param name="msg">日志内容</param>
        /// <param name="IsAutoDelete">是否自动删除日志</param>
        private static void writeInLog(string msg, bool IsAutoDelete)
        {
            IsAutoDelete = false;
            try
            {
                var fileinfo = new FileInfo(_logFileName);
                if (IsAutoDelete)
                {
                    if (fileinfo.Exists && fileinfo.Length >= _logFileSizes)
                    {
                        fileinfo.Delete();
                    }
                }
                using (var fs = fileinfo.OpenWrite())
                {
                    var sw = new StreamWriter(fs);
                    sw.BaseStream.Seek(0, SeekOrigin.End);
                    sw.Write(DateTime.Now + "\r\n");
                    sw.Write("日志内容:" + msg + "\r\n");
                    sw.Flush();
                    sw.Close();
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}
